{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(10734, 101)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#导入必要的工具包\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn import svm\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.decomposition import PCA\n",
    "import time\n",
    "#读取evnets\n",
    "train = pd.read_csv('events_train_test.csv')\n",
    "\n",
    "y_train=train.event_id.values[:]   \n",
    "from sklearn.preprocessing import LabelEncoder\n",
    "labelencoder=LabelEncoder()\n",
    "   \n",
    "y_train = labelencoder.fit_transform(y_train)\n",
    "X_train=train.drop(['event_id','user_id','start_time','city','state','zip','country','lat','lng'],axis=1)\n",
    "# 将训练集合拆分成训练集和校验集，在校验集上找到最佳的模型超参数（PCA的维数）\n",
    "X_train_part, X_val, y_train_part, y_val = train_test_split(X_train,y_train, train_size = 0.8,random_state = 0)\n",
    "X_train_part.shape\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2684, 101)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_val.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.7        0.71071429 0.72142857 0.73214286 0.74285714 0.75357143\n",
      " 0.76428571 0.775      0.78571429 0.79642857 0.80714286 0.81785714\n",
      " 0.82857143 0.83928571 0.85      ]\n",
      "PCA begin with n_components: 0.7\n",
      "SVC begin\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# 一个参数点（PCA维数为n）的模型训练和测试，得到该参数下模型在校验集上的预测性能\n",
    "def n_component_analysis(n, X_train, y_train, X_val, y_val):\n",
    "    start = time.time()\n",
    "    \n",
    "    pca = PCA(n_components=n)\n",
    "    print(\"PCA begin with n_components: {}\".format(n));\n",
    "    pca.fit(X_train)\n",
    "    \n",
    "    # 在训练集和测试集降维 \n",
    "    X_train_pca = pca.transform(X_train)\n",
    "    X_val_pca = pca.transform(X_val)\n",
    "    \n",
    "    # 利用SVC训练\n",
    "    print('SVC begin')\n",
    "    clf1 = svm.SVC()\n",
    "    clf1.fit(X_train_pca, y_train)\n",
    "    \n",
    "    # 返回accuracy\n",
    "    accuracy = clf1.score(X_val_pca, y_val)\n",
    "    \n",
    "    end = time.time()\n",
    "    print(\"accuracy: {}, time elaps:{}\".format(accuracy, int(end-start)))\n",
    "    return accuracy\n",
    "# 设置超参数（PCA维数）搜索范围\n",
    "n_s = np.linspace(0.70, 0.85, num=15)\n",
    "print(n_s)\n",
    "accuracy = []\n",
    "for n in n_s:\n",
    "    tmp = n_component_analysis(n, X_train_part, y_train_part, X_val, y_val)\n",
    "    accuracy.append(tmp)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
